package com.cevent.community.app.template.generator;/**
 * Created by Cevent on 2021/9/15.
 */

import com.cevent.community.app.template.utils.DataBaseUtil;
import com.cevent.community.app.template.utils.FieldUtil;
import com.cevent.community.app.template.utils.FreemarkerUtil;
import freemarker.template.TemplateException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author cevent
 * @description 前端vue页面自动生成器
 * @date 2021/9/15 18:32
 */
public class VueApplication {
    private static final Logger LOG= LoggerFactory.getLogger(VueApplication.class);
    //生成的模块目标路径节点
    static String PARENT_DIR="com_cevent_communit_app";
    static String MODULE_NAME="business";
    static String COMMON_NAME="common";
    static String VUE_NAME="vue.ftl";
    static String VUE_EDIT_NAME="editVue.ftl";
    static String EDIT_NAME="Edit";
    //生成文件目标的路径
    static String targetVuePath="view_client_template\\src\\views\\module\\";
    //扫描数据库table配置
    static String generatorConfig=PARENT_DIR+"\\"+COMMON_NAME+"\\src\\main\\resources\\generator\\generatorConfig.xml";

    public static void main(String[] args) throws DocumentException, SQLException, IOException, TemplateException {
        //1.读取xml配置文件
        File file=new File(generatorConfig);
        //引入dom4j进行读取
        SAXReader reader=new SAXReader();
        Document document=reader.read(file);
        //2.读取xml文件的根节点/context上下文节点
        Element rootElement=document.getRootElement();
        Element contextElement=rootElement.element("context");
        //3.定义tableElement遍历获取表
        Element tableElement;
        tableElement=contextElement.elementIterator("table").next();
        //对应xml配置文件中的：domainObjectName="TableName"
        String Domain=tableElement.attributeValue("domainObjectName");
        String domain=Domain.substring(0,1).toLowerCase()+Domain.substring(1);
        String tableName=tableElement.attributeValue("tableName");
        String tableNameCN= DataBaseUtil.getTableComment(tableName);
        LOG.info("freemarker -> VueGenerator自动生成vue前端页面表名：{}"
                ,tableElement.attributeValue("tableName"));
        LOG.info("大写Domain：{}",tableElement.attributeValue("domainObjectName"));

        //4.获取所有列信息
        List<FieldUtil> fieldUtilList=DataBaseUtil.transferColumnByTableName(tableName);
        //根据fieldUtil类信息，获取所有的set类型集合，typeSet用于ftl中获取java类型集合
        Set<String> typeSet=ControllerApplication.getJavaTypes(fieldUtilList);
        Map<String,Object> map=new HashMap<>();
        map.put("Domain",Domain);
        map.put("domain",domain);
        map.put("tableNameCN",tableNameCN);
        map.put("module",MODULE_NAME);
        map.put("fieldUtilList",fieldUtilList);
        map.put("typeSet",typeSet);
        //6.初始化vue
        FreemarkerUtil.initConfig(VUE_NAME);
        LOG.info("Vue代码自动生成器，初始化完毕！Domain：{}",Domain);
        //列表模板
        FreemarkerUtil.freemarkerGenerator(targetVuePath+Domain+".vue",map);
        //编辑模板
        //FreemarkerUtil.freemarkerGenerator(targetVuePath+EDIT_NAME+Domain+".vue",map);
    }
}
